/**
 * Created by Administrator on 2015/1/13.
 */
function minesweeper() {
    this.d = [[-1, -1], [0, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1]];
    this.types = {
        "0": "Images/Cells/cell.gif",
        "1": "Images/Cells/1.gif",
        "2": "Images/Cells/2.gif",
        "3": "Images/Cells/3.gif",
        "4": "Images/Cells/4.gif",
        "5": "Images/Cells/5.gif",
        "6": "Images/Cells/6.gif",
        "7": "Images/Cells/7.gif",
        "8": "Images/Cells/8.gif",
        "*": "Images/Cells/mine.gif",
        "#": "Images/Cells/flag.gif",
        "@": "Images/Cells/empty.gif"
    };

    this.show();
    //this.select();
    //this.init();
}

// 显示按钮选项
minesweeper.prototype.show = function () {
    this.btn = dom("button", null, "确定");
    this.btn.addEventListener("click", method(this, "select"));

    this.difficulty1 = dom("input", {type: "radio", name: "difficulty", id:"1", checked: "true"});
    this.difficulty2 = dom("input", {type: "radio", name: "difficulty", id:"2"});
    this.difficulty3 = dom("input", {type: "radio", name: "difficulty", id:"3"});
    this.difficulty = dom("div", null, dom("b", null, "难度："),
        this.difficulty1, dom("b", null, "初级"),
        this.difficulty2, dom("b", null, "中级"),
        this.difficulty3, dom("b", null, "高级"), this.btn);
    document.body.appendChild(this.difficulty);

    this.time = dom("div", {id:"time"});
    document.body.appendChild(this.time);

    this.mine = dom("div", {id:"mine"});
    document.body.appendChild(this.mine);

    this.div = dom("div", null);
    document.body.appendChild(this.div);
}

//难度选择确定
minesweeper.prototype.select = function () {
    if (document.getElementById("1").checked) {
        this.row = 9;
        this.col = 9;
        this.count = 10;
        this.c = 10;
    } else if (document.getElementById("2").checked) {
        this.row = 16;
        this.col = 16;
        this.count = 40;
        this.c = 40;
    } else if (document.getElementById("3").checked) {
        this.row = 16;
        this.col = 30;
        this.count = 99;
        this.c = 99;
    }
    this.init();
}

//初始化
minesweeper.prototype.init = function () {
    this.div.innerHTML = "";

    this.initmap();
    this.showmap();
    this.laymines();
    this.click();
    this.showmap();
    clearTimeout(t);
    timecount(0);
    this.minecount();
}

//初始化地图
minesweeper.prototype.initmap = function () {
    this.map = new Array();
    for (var i = 0; i < this.row; i++) {
        this.map[i] = new Array();
        for (var j = 0; j < this.col; j++) {
            this.map[i][j] = new mine(0, "notopen", null);
        }
    }
}

//布雷
minesweeper.prototype.laymines = function () {
    for (var i = 0; i < this.count; i++) {
        var r1 = parseInt(Math.random() * (this.row - 1));
        var r2 = parseInt(Math.random() * (this.col - 1));
        if (this.map[r1][r2].type != "*") {
            this.map[r1][r2].type = "*";
            for (var j = 0; j < 8; j++) {
                if ((r1 + this.d[j][0] >= 0 && r1 + this.d[j][0] < this.row) && (r2 + this.d[j][1] >= 0 && r2 + this.d[j][1] < this.col)) {
                    if (this.map[r1 + this.d[j][0]][r2 + this.d[j][1]].type != "*") {
                        this.map[r1 + this.d[j][0]][r2 + this.d[j][1]].type++;
                    }
                }
            }
        } else {
            i--;
        }
    }
}

//显示布局
minesweeper.prototype.showmap = function () {
    this.div.appendChild(dom("br", null));
    for (var i = 0; i < this.row; i++) {
        for (var j = 0; j < this.col; j++) {
            var img = document.createElement("img");
            img.id = i + "," + j;
            img.src = this.types[this.map[i][j].type];
            this.map[i][j].img = img;
            this.div.appendChild(img);
        }
        this.div.appendChild(dom("br", null));
    }
}

//点击绑定
minesweeper.prototype.click = function () {
    var minesweeper = this;
    for (var i = 0; i < this.row; i++) {
        for (var j = 0; j < this.col; j++) {
            registerEventHandler(this.map[i][j].img, "click", function (e) {
                var k = e.target.id.split(",");
                minesweeper.onleftclick(k[0], k[1]);
            });
            this.map[i][j].img.oncontextmenu = function (e) {
                e.preventDefault();
                var k = e.target.id.split(",");
                minesweeper.onrightclick(k[0], k[1]);
            }
        }
    }
}

//左键点击事件
minesweeper.prototype.onleftclick = function (i, j) {
    if (this.map[i][j].state == "notopen") {
        if (this.map[i][j].type == 0) {
            this.turn(i, j);
        } else if (this.map[i][j].type == "*"){
            this.map[i][j].state = "open";
            this.openmap(i, j);
            this.isover(true);
        } else {
            this.map[i][j].state = "open";
            this.openmap(i, j);
        }
    }
}

//右键点击事件
minesweeper.prototype.onrightclick = function (i, j) {
    if (this.map[i][j].state == "notopen") {
        if (this.map[i][j].type == "*") {
            this.c--;
        }
        this.count--;
        this.minecount();
        this.map[i][j].state = "flag";
        this.openmap(i, j);
    } else if (this.map[i][j].state == "flag") {
        if (this.map[i][j].type == "*") {
            this.c++;
        }
        this.count++;
        this.minecount();
        this.map[i][j].state = "notopen";
        this.openmap(i, j);
    }
    this.isover(false);
}

//空白区块翻转
minesweeper.prototype.turn = function (i, j) {
    var minesweeper = this;
    if (!(i >= 0 && i < this.row && j >= 0 && j < this.col)) {
        return;
    }

    if (this.map[i][j].type == 0 && this.map[i][j].state == "notopen") {
        this.map[i][j].state = "open";
        this.map[i][j].type = "@";
        this.openmap(i, j);

        for (var k = 0; k < 8; k++) {
            minesweeper.turn(parseInt(i) + this.d[k][0], parseInt(j) + this.d[k][1]);
        }
    }

    if (this.map[i][j].type != 0 && this.map[i][j].state == "notopen") {
        this.map[i][j].state = "open";
        this.openmap(i, j);
    }
}

//区块图片更改
minesweeper.prototype.openmap = function (i, j) {
    if (this.map[i][j].state == "flag") {
        var img = document.getElementById(i + "," + j);
        img.src = this.types["#"];
        this.map[i][j].img = img;
    } else if (this.map[i][j].state == "notopen") {
        var img = document.getElementById(i + "," + j);
        img.src = this.types[0];
        this.map[i][j].img = img;
    } else {
        var img = document.getElementById(i + "," + j);
        img.src = this.types[this.map[i][j].type];
        this.map[i][j].img = img;
    }
}

//判断游戏是否结束
minesweeper.prototype.isover = function (over) {
    if (over) {
        alert("失败");
        this.init();
    } else if (this.count == 0) {
        alert("成功");
        this.init();
    }
}

//计时器
var t;
function timecount(tc) {
    document.getElementById("time").innerHTML = "用时为：   " + tc + "  秒";
    tc++;
    t = setTimeout("timecount(" + tc + ")", 1000);
}

//雷数统计
minesweeper.prototype.minecount = function () {
    document.getElementById("mine").innerHTML = "剩余雷数为： " + this.count + " 个";
}